package com.dotstone.chipism.activity;

import org.json.JSONArray;
//import org.json.JSONException;
//import org.json.JSONObject;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.x;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import com.dotstone.chipism.R;
import com.dotstone.chipism.bean.Device;
import com.dotstone.chipism.bean.DeviceType;
import com.dotstone.chipism.bean.Home;
import com.dotstone.chipism.bean.HttpURL;
import com.dotstone.chipism.bean.MySocket1;
import com.dotstone.chipism.bean.MySocket1.OnConnectSuccessListener;
import com.dotstone.chipism.bean.MySocket1.RecDataCallBackListener_Bind;
import com.dotstone.chipism.bean.WlanInfo;
import com.dotstone.chipism.dialog.SVProgressHUD;
import com.dotstone.chipism.esptouch.EsptouchTask;
import com.dotstone.chipism.esptouch.IEsptouchResult;
import com.dotstone.chipism.esptouch.IEsptouchTask;
import com.dotstone.chipism.ui.login.LoginActivity;
import com.dotstone.chipism.util.ConvertUtil;
import com.dotstone.chipism.util.DeviceManager;
import com.dotstone.chipism.util.NetUtil;
import com.dotstone.chipism.util.SPUtils;
import com.dotstone.chipism.util.SocketManager;
import com.dotstone.chipism.util.ToastShow;
import com.dotstone.chipism.util.WifiAdmin;
import com.dotstone.chipism.view.BaseAnimatorSet;
import com.dotstone.chipism.view.NormalDialog;
import com.dotstone.chipism.view.OnBtnClickL;
import com.dotstone.chipism.view.slidingfragmenu.BarUtils;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class PairWlanActivity1 extends BaseActivity {
	private LinearLayout mBackL;
	private TextView mStatusBar;
	private String ssid;
	private String password;
	private boolean noPwd;
	private boolean pairing = true;
	private String mWifiBSSID;
	private MySocket1 mySocket;
	private int time;
	private String deviceId = "";
	private TextView mProgressTv;
	private TextView mReason;
	private LinearLayout mPairL;
	private LinearLayout mFailureL;
	private LinearLayout mSuccessL;
	private Button mNext;
	private Button mNext1;
	private EditText mNameEd;
	private String mDeviceName;
	private NormalDialog mNormalDialog;
	private BaseAnimatorSet bas_in;
	private BaseAnimatorSet bas_out;
	private SVProgressHUD mSvProgressHUD;
	private boolean needToShow = true;
	/**
	 * 0获取BSSID 1 建立socket连接 2 获取deviceId 3发送deviceId 4修改名称
	 */
	private int step = 0;// 0 获取BSSID 1 建立socket连接 2 获取deviceId 3发送deviceId
							// 4修改名称

	@Override
	public void widgetClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back_layout:
			if (!pairing) {
				finish();
			} else {
				if (!deviceId.equals("")) {
					exit1();
				} else {
					exit();
				}
			}
			break;
		case R.id.nextstep1:
			mDeviceName = mNameEd.getText().toString();
			boolean e = DeviceManager.getInstance().checkName(mDeviceName);
			if (e) {
				ToastShow.Show(getApplicationContext(), getString(R.string.device_exists1));
				return;
			}
			if (mDeviceName.isEmpty() || mDeviceName.length() < 2 || mDeviceName.length() > 6) {
				ToastShow.Show(getApplicationContext(), getString(R.string.device_name_not_vaild));
			} else {
				updateName(mDeviceName);
			}
			break;
		case R.id.nextstep2:
			if (mySocket != null) {
				new Thread(new Runnable() {
					public void run() {
						mySocket.setStop(true);
						mySocket.stopLocalTcpClient();
						mySocket = null;
					}
				}).start();

			}
			finish();
			break;

		default:
			break;
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (!pairing) {
				finish();
			} else {
				exit();
			}
			return false;
		}
		return super.onKeyDown(keyCode, event);
	}

	private void exit() {
		mNormalDialog = new NormalDialog(PairWlanActivity1.this);
		mNormalDialog.content("您正在配置网关，是否需要退出？").style(NormalDialog.STYLE_TWO)//
				.titleTextSize(18)//
				.showAnim(bas_in)//
				.dismissAnim(bas_out)//
				.show();
		mNormalDialog.setOnBtnClickL(new OnBtnClickL() {
			@Override
			public void onBtnClick() {
				mNormalDialog.dismiss();
			}
		}, new OnBtnClickL() {
			@Override
			public void onBtnClick() {
				mNormalDialog.dismiss();
				if (mySocket != null) {
					new Thread(new Runnable() {
						public void run() {
							mySocket.setStop(true);
							mySocket.stopLocalTcpClient();
							mySocket = null;
						}
					}).start();
				}
				deleteDevie(deviceId);
				PairWlanActivity.pairWlanActivity.finish();
				finish();
			}

		});
	}

	private void exit1() {
		mNormalDialog = new NormalDialog(PairWlanActivity1.this);
		mNormalDialog.content("您尚未保存网关，是否需要退出？").style(NormalDialog.STYLE_TWO)//
				.titleTextSize(18)//
				.showAnim(bas_in)//
				.dismissAnim(bas_out)//
				.show();
		mNormalDialog.setOnBtnClickL(new OnBtnClickL() {
			@Override
			public void onBtnClick() {
				mNormalDialog.dismiss();
			}
		}, new OnBtnClickL() {
			@Override
			public void onBtnClick() {
				mNormalDialog.dismiss();
				Log.i("wmy", "删除设备" + deviceId);
				deleteDevie(deviceId);
			}

		});
	}

	@Override
	public void initParms(Bundle parms) {
		// TODO Auto-generated method stub

	}

	@Override
	public View bindView() {
		// TODO Auto-generated method stub
		return null;
	}

	protected void onResume() {
		super.onResume();
		if (SocketManager.getInstance().pairing && needToShow) {
			mSvProgressHUD = new SVProgressHUD(PairWlanActivity1.this);
			mSvProgressHUD.showWithStatus("正在获取BSSID");
		} else {
			if (mSvProgressHUD.isShowing()) {
				mSvProgressHUD.dismiss();
			}
		}
		getHouse();
	};
	
	private void getHouse() {
		RequestParams params = new RequestParams(HttpURL.GET_HOME_URL);
		params.setAsJsonContent(true);
		JSONObject obj = new JSONObject();
		try {
			obj.put("key", "10000");
			obj.put("token", (String)SPUtils.get(getApplicationContext(), "token", ""));
			obj.put("requestTime", "" + System.currentTimeMillis());
			Log.d("test", "login obj = " + obj.toString());
		} catch (JSONException e1) {
			e1.printStackTrace();
		}
		params.setBodyContent(obj.toString());

		x.http().post(params, new Callback.CommonCallback<String>() {
			@Override
			public void onSuccess(String result) {
				Log.e("Tag", "成功=" + result);
				try {
					JSONObject jsonObject = new JSONObject(result);

					int resultCode = jsonObject.optInt("resultCode");

					if (resultCode == 1) {
						JSONArray ja = jsonObject.optJSONArray("data");
						if (ja.length() < 1) {
							
						} else {
							for (int i = 0; i < ja.length(); i++) {
								String hid = ja.getJSONObject(i).getString("hid");
								String userId = ja.getJSONObject(i).getString("userId");
								String homeName = ja.getJSONObject(i).getString("homeName");
								int status = ja.getJSONObject(i).getInt("status");
								if (status == 1) {
									DeviceManager.getInstance().homeName = homeName;
									DeviceManager.getInstance().hid = hid;
								}
								Home home = new Home(hid, userId, homeName, status);
								DeviceManager.getInstance().addHome(home);
							}
						}

					} else {
//						
						throw new Exception();
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

			@Override
			public void onError(Throwable ex, boolean isOnCallback) {
				Log.e("Tag", "错误" + ex.getMessage());
				// 清空所有本地数据，进入MainActivity
				SPUtils.clear(getApplicationContext());
				Intent intent = new Intent(PairWlanActivity1.this, LoginActivity.class);
				startActivity(intent);
				finish();
			}

			@Override
			public void onCancelled(CancelledException cex) {

			}

			@Override
			public void onFinished() {
				Log.e("Tag", "完成");
			}
		});
	}

	Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 0:
				// 配置成功 创建socket连接
				step = 1;
				mSvProgressHUD.setText("获取网关bssid:" + mWifiBSSID);
				mHandler.postDelayed(new Runnable() {

					@Override
					public void run() {
						// TODO Auto-generated method stub
						WlanInfo wlanInfo = new WlanInfo("DotStone", "10000000", mWifiBSSID, "10000", 0);
						mySocket = new MySocket1(wlanInfo, PairWlanActivity1.this);
						setConnectSuccessListener();
						setBindSuccessListener();
						// 开启计时 30秒后如果尚未配置成功 则跳转 断开连接
						startFailListener();
					}
				}, 5000);
				break;
			case 1:
				// 连接成功 向服务器请求DeviceId
				step = 2;
				mSvProgressHUD.setText("连接成功 向服务器请求DeviceId");
				upload();
				break;
			case 2:
				// 获取DeviceId成功，向网关绑定
				step = 3;
				mSvProgressHUD.setText("获取DeviceId成功，绑定网关");
				sendBindIdCommand(deviceId);
				break;
			case 3:
				// 绑定成功，切换界面
				step = 4;
				SocketManager.getInstance().pairing = false;
				if (mSvProgressHUD.isShowing()) {
					mSvProgressHUD.dismiss();
				}
				mProgressTv.setText(getString(R.string.pair_success));
				// mPairL.setVisibility(View.GONE);
				needToShow = false;
				mFailureL.setVisibility(View.GONE);
				mSuccessL.setVisibility(View.VISIBLE);
				mNameEd.setSelection(mNameEd.getText().toString().length());
				if (mySocket != null) {
					new Thread(new Runnable() {
						public void run() {
							mySocket.setStop(true);
							mySocket.stopLocalTcpClient();
							mySocket = null;
						}
					}).start();
				}
				break;
			case 4:
				Toast.makeText(getApplicationContext(), "保存成功！", Toast.LENGTH_SHORT).show();
				if (mySocket != null) {
					new Thread(new Runnable() {
						public void run() {
							mySocket.setStop(true);
							mySocket.stopLocalTcpClient();
							mySocket = null;
						}
					}).start();
				}
				SocketManager.getInstance().pairing = false;
				PairWlanActivity.pairWlanActivity.finish();
				Device d = new Device();
				d.setDeviceType(DeviceType.DEVICE_TYPE_WLAN);
				d.setDeviceName(mNameEd.getText().toString());
				d.setDeviceID(deviceId);
				DeviceManager.getInstance().setmSaveWlan(d);
				if (AddTypeSelectionActivity1.addTypeSelectionActivity1 != null) {
					AddTypeSelectionActivity1.addTypeSelectionActivity1.finish();
				}
				finish();
				break;
			case 11:
				SocketManager.getInstance().pairing = false;
				if (mySocket != null) {
					new Thread(new Runnable() {
						public void run() {
							mySocket.setStop(true);
							mySocket.stopLocalTcpClient();
							mySocket = null;
						}
					}).start();
				}
				if (mSvProgressHUD.isShowing()) {
					mSvProgressHUD.dismiss();
				}
				pairing = false;
				mProgressTv.setText(getString(R.string.pair_fail));
				setReason();
				if(msg.obj!=null){
					ToastShow.Show(getApplicationContext(),getString(R.string.pair_fail)+msg.obj.toString());
				}
				mPairL.setVisibility(View.GONE);
				mFailureL.setVisibility(View.VISIBLE);
				mSuccessL.setVisibility(View.GONE);
				break;
			default:
				break;
			}
		}
	};

	@Override
	public int bindLayout() {
		// TODO Auto-generated method stub
		
		return R.layout.activity_pair_wlan1;
	}

	@Override
	public void initView(View view) {
		// TODO Auto-generated method stub
		mBackL = $(R.id.back_layout);
		mStatusBar = $(R.id.tv_statusBar_mian);
		mProgressTv = $(R.id.progress_tv);
		mPairL = $(R.id.pair_l);
		mSuccessL = $(R.id.success_l);
		mFailureL = $(R.id.fail_l);
		mNext = $(R.id.nextstep1);
		mNext1 = $(R.id.nextstep2);
		mNameEd = $(R.id.name_ed);
		mReason = $(R.id.reason);
		boolVersion();
		mNext.setOnClickListener(this);
		mNext1.setOnClickListener(this);
		mBackL.setOnClickListener(this);
		initData();
	}

	private void initData() {
		// TODO Auto-generated method stub
		ssid = getIntent().getStringExtra("ssid");
		password = getIntent().getStringExtra("password");
		noPwd = getIntent().getBooleanExtra("nopwd", false);
		pair();
	}

	public void pair() {
		SocketManager.getInstance().pairing = true;
		startPair();
	}

	/**
	 * 方法：connectWifi 用途：通过wifi名 wifi的ssid 以及wifi密码 获取bssid
	 */
	public void startPair() {
		// 从控件中获取 wifi的ssid wifi密码的文本内容 ，从wifi管理工具类获取mac地址
		SocketManager.getInstance().pairing = true;
		String mWifiBSSID = new WifiAdmin(getApplicationContext()).getWifiConnectedBssid();
		if (noPwd) {
			Log.i("wmy", "配置无密码 ");
			new EsptouchAsyncTask2().execute(ssid, mWifiBSSID, "", "NO", "4");
		} else {
			Log.i("wmy", "配置有密码 ");
			new EsptouchAsyncTask2().execute(ssid, mWifiBSSID, password, "NO", "4");
		}
	}

	private class EsptouchAsyncTask2 extends AsyncTask<String, Void, IEsptouchResult> {

		private IEsptouchTask mEsptouchTask;
		// without the lock, if the user tap confirm and cancel quickly enough,
		// the bug will arise. the reason is follows:
		// 0. task is starting created, but not finished
		// 1. the task is cancel for the task hasn't been created, it do nothing
		// 2. task is created
		// 3. Oops, the task should be cancelled, but it is running
		private final Object mLock = new Object();

		/**
		 * 执行前
		 */
		@Override
		protected void onPreExecute() {

		}

		/**
		 * 在后台执行
		 */
		@Override
		protected IEsptouchResult doInBackground(String... params) {
			synchronized (mLock) {
				String apSsid = params[0];
				String apBssid = params[1];
				String apPassword = params[2];
				String isSsidHiddenStr = "No";
				Log.i("wmy", params.toString());
				boolean isSsidHidden = false;
				if (isSsidHiddenStr.equals("YES")) {
					isSsidHidden = true;
				}
				mEsptouchTask = new EsptouchTask(apSsid, apBssid, apPassword, isSsidHidden, getApplicationContext());
				// mEsptouchTask.setEsptouchListener(myListener);
			}
			IEsptouchResult result = mEsptouchTask.executeForResult();
			return result;
		}

		/**
		 * 执行完成，已经连接成功
		 */
		@Override
		protected void onPostExecute(IEsptouchResult result) {
			Log.i("Tag", "onPostExecute");
			// it is unnecessary at the moment, add here just to show how to
			// use isCancelled()
			if (!result.isCancelled()) {
				if (result.isSuc()) {
					Log.i("wmy", "Esptouch success, bssid123 = " + result.getBssid() + ",InetAddress = "
							+ result.getInetAddress().getHostAddress());
					mWifiBSSID = result.getBssid();
					// createSocketforBind();
					Message msg = Message.obtain();
					msg.what = 0;
					// mHandler.sendMessageDelayed(msg, 5 * 1000);
					mHandler.sendMessage(msg);
				} else {
					if (SocketManager.getInstance().pairing) {
						SocketManager.getInstance().pairing = false;
						Message msg = Message.obtain();
						msg.what = 11;
						Log.i("wmy", "365添加失败");
						mHandler.sendMessage(msg);
					}
				}
			}
		}
	}

	@Override
	public void setListener() {
		// TODO Auto-generated method stub

	}

	@Override
	public void doBusiness(Context mContext) {
		// TODO Auto-generated method stub

	}

	private void setReason() {
		switch (step) {
		case 0:
			mReason.setText("获取网关BSSID失败");
			break;

		case 1:
			mReason.setText("与网关建立连接失败");
			break;

		case 2:
			mReason.setText("向服务器请求设备ID失败");
			break;
		case 3:
			mReason.setText("向网关绑定设备ID失败");
			break;
		default:
			break;
		}
	}

	public void boolVersion() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

			Window window = getWindow();
			window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
					WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
			// 透明导航栏
			// getWindow().addFlags(
			// WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
			// 改变titlebar的高度
			int statusbarHeight = BarUtils.getStatusBarHeight(this);
			RelativeLayout.LayoutParams layoutParams1 = (RelativeLayout.LayoutParams) mStatusBar.getLayoutParams();
			layoutParams1.height = statusbarHeight;
			mStatusBar.setLayoutParams(layoutParams1);
			mStatusBar.setVisibility(View.VISIBLE);
		} else {
			mStatusBar.setVisibility(View.GONE);
		}
	}

	private void setConnectSuccessListener() {
		mySocket.setOnConnectSuccessListener(new OnConnectSuccessListener() {

			@Override
			public void onReceiveData(String mac) {
				// TODO Auto-generated method stub
				Log.i("wmy", mac + "连接成功");
				Message msg = Message.obtain();
				msg.what = 1;
				mHandler.sendMessage(msg);
			}
		});
	}

	private void setBindSuccessListener() {
		mySocket.setRecDataCallBackListener_Bind(new RecDataCallBackListener_Bind() {

			@Override
			public void onReceiveData(String data) {
				// TODO Auto-generated method stub
				Log.i("wmy", "绑定成功");
				Message msg = Message.obtain();
				msg.what = 3;
				mHandler.sendMessage(msg);
			}
		});
	}

	public void sendBindIdCommand(final String deviceId) {
		final String sendCmd = ConvertUtil.getInstance().convertBindIdCmd(deviceId);
		if (mySocket != null) {
			new Thread(new Runnable() {

				@Override
				public void run() {
					// TODO Auto-generated method stub
					mySocket.sendMqttData(sendCmd);
				}
			}).start();
		}
	}

	private void startFailListener() {
		time = 0;
		new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				while (SocketManager.getInstance().pairing) {
					time++;
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					if (time > 30) {
						if (SocketManager.getInstance().pairing) {
							SocketManager.getInstance().pairing = false;
							Message msg = Message.obtain();
							msg.what = 11;
							Log.i("wmy", "471添加失败");
							mHandler.sendMessage(msg);
						}
					}
				}
			}
		}).start();
	}

	private void deleteDevie(String deviceId) {
		if (!NetUtil.isNetworkAvailable(getApplicationContext())) {
			Log.i("wmy", "518退出");
			finish();
			return;
		}
		RequestParams params = new RequestParams(HttpURL.DELETE_DEVICE_URL);
		params.setAsJsonContent(true);
		JSONObject obj = new JSONObject();
		try {
			obj.put("deviceId", deviceId);
			obj.put("key", "10000");
			obj.put("requestTime", "" + System.currentTimeMillis());
			obj.put("token", (String) SPUtils.get(getApplicationContext(), "token", ""));
		} catch (JSONException e1) {
			e1.printStackTrace();
		}
		params.setBodyContent(obj.toString());
		x.http().post(params, new Callback.CommonCallback<String>() {
			@Override
			public void onSuccess(String result) {
				Log.e("wmy", "delete =" + result);
				// deleteLocal();
				try {
					JSONObject jsonObject = new JSONObject(result);
					String resultCode = jsonObject.getString("resultCode");
					if (resultCode.equals("1")) {
						finish();
					} else {
						throw new Exception();
					}
				} catch (Exception e) {
					e.printStackTrace();
					// deleteLocal();
					finish();
				}
			}

			@Override
			public void onCancelled(CancelledException arg0) {

			}

			@Override
			public void onError(Throwable arg0, boolean arg1) {

			}

			@Override
			public void onFinished() {

			}
		});

	}

	private void upload() {
		new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				RequestParams params = new RequestParams(HttpURL.UPLOAD_DEVICE_URL);
				params.setAsJsonContent(true);
				JSONObject obj = new JSONObject();
				try {
					obj.put("classification", "1");
					obj.put("hid", DeviceManager.getInstance().hid);
					obj.put("mainDeviceId", "0");
					obj.put("productId", "1");
					mWifiBSSID = mWifiBSSID.toUpperCase();
					obj.put("deviceAddress", mWifiBSSID);
					obj.put("deviceModel", "DotStone10000000");
					obj.put("deviceName", "芯主义网关");
					obj.put("deviceType", DeviceType.DEVICE_TYPE_WLAN + "");
					obj.put("deviceVersion", "1.0");	
					obj.put("deviceWifiSSID", ssid);
					obj.put("deviceId", "0");
					obj.put("key", "10000");
					obj.put("requestTime", "" + System.currentTimeMillis());
					obj.put("token", (String) SPUtils.get(getApplicationContext(), "token", ""));
					Log.e("wmy", "get deviceId obj.toString()= "+obj.toString());
				} catch (JSONException e1) {
					e1.printStackTrace();
				}
				params.setBodyContent(obj.toString());
				x.http().post(params, new Callback.CommonCallback<String>() {
					@Override
					public void onSuccess(String result) {
						Log.e("wmy", "get deviceId result= " + result);
						try {
							JSONObject jsonObject = new JSONObject(result);
							String value = jsonObject.getString("resultCode");
							if (value.equals("1")) {
								Log.i("wmy", "请求成功 = " + result);
								deviceId = jsonObject.getString("data");
								Message msg = Message.obtain();
								msg.what = 2;
								mHandler.sendMessage(msg);
							}else{
								String errorMsg = jsonObject.getString("errorMsg");
								Message msg = Message.obtain();
								msg.what = 11;
								msg.obj = errorMsg;
								mHandler.sendMessage(msg);
							}
						} catch (JSONException e) {
							e.printStackTrace();
						}
					}

					@Override
					public void onCancelled(CancelledException arg0) {
						Log.e("wmy", "onCancelled");
					}

					@Override
					public void onError(Throwable arg0, boolean arg1) {
						Log.e("wmy", "onError");
						upload();
					}

					@Override
					public void onFinished() {
						Log.e("wmy", "onFinished");
					}
				});

			}
		}).start();
	}

	private void updateName(final String mDeviceName) {
		new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				RequestParams params = new RequestParams(HttpURL.UPDATE_DEVICE_URL);
				params.setAsJsonContent(true);
				JSONObject obj = new JSONObject();
				try {
					obj.put("deviceName", mDeviceName);
					obj.put("deviceId", deviceId);
					obj.put("key", "10000");
					obj.put("requestTime", "" + System.currentTimeMillis());
					obj.put("token", (String) SPUtils.get(getApplicationContext(), "token", ""));
				} catch (JSONException e1) {
					e1.printStackTrace();
				}
				Log.i("wmy", "上传修改名称的数据" + obj.toString());
				params.setBodyContent(obj.toString());
				x.http().post(params, new Callback.CommonCallback<String>() {
					@Override
					public void onSuccess(String result) {
						Log.e("wmy", "修改名字 = " + result);
						try {
							JSONObject jsonObject = new JSONObject(result);
							String value = jsonObject.getString("resultCode");
							if (value.equals("1")) {
								Log.i("wmy", "请求成功 = " + result);
								Message msg = Message.obtain();
								msg.what = 4;
								msg.arg1 = 1;
								mHandler.sendMessage(msg);
							} else {
								Log.i("wmy", "请求失败");
								Message msg = Message.obtain();
								msg.what = 4;
								msg.arg1 = 2;
								mHandler.sendMessage(msg);
							}
						} catch (JSONException e) {
							e.printStackTrace();
						}
					}

					@Override
					public void onCancelled(CancelledException arg0) {

					}

					@Override
					public void onError(Throwable arg0, boolean arg1) {

					}

					@Override
					public void onFinished() {

					}
				});
			}
		}).start();
	}

}
